﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NeonMika.Webserver.ClientLibrary;

namespace NeonMika.NetduinoControl
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Netduino netduino;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            netduino = new Netduino();
            netduino.IP = NeonMika.NetduinoControl.Properties.Settings.Default.NetduinoIP;
            this.DataContext = netduino;
        }

        private void ButtonRawCommand_Click(object sender, RoutedEventArgs e)
        {
            XmlCommand command = new XmlCommand(TextBoxRawCommand.Text, "Raw command", null);
            command.Execute(netduino.IP);
            TextBoxLastResponse.Text = command.LastResponse;
        }

        private void ButtonSwitchPin_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBoxSwitchPin.SelectedIndex > 0)
            {
                //SelectedIndex - 1 because the first item is the "text-item" that displays "Select pin"
                TextBoxLastResponse.Text = netduino.SwitchPin(ComboBoxSwitchPin.SelectedIndex - 1);
            }
        }

        private void ButtonSetPin_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBoxSetPinPin.SelectedIndex > 0)
            {
                //SelectedIndex - 1 because the first item is the "text-item" that displays "Select pin"
                TextBoxLastResponse.Text = netduino.SetDigitalPin(ComboBoxSetPinPin.SelectedIndex - 1, (bool)TextBoxSetPinState.IsChecked);
            }
        }

        private void ButtonUpdateAllPins_Click(object sender, RoutedEventArgs e)
        {
            netduino.UpdateAll();
        }

        private void ButtonUpdateDigitalPins_Click(object sender, RoutedEventArgs e)
        {
            TextBoxLastResponse.Text = netduino.UpdateDigitalPins();
        }

        private void ButtonUpdateAnalogPins_Click(object sender, RoutedEventArgs e)
        {
            TextBoxLastResponse.Text = netduino.UpdateAnalogPins();
        }

        private void SliderDuration_MouseUp(object sender, MouseButtonEventArgs e)
        {
            TextBoxLastResponse.Text = netduino.DigitalPins[Int32.Parse(((Slider)sender).Uid)].SetPWM();
        }

        private void ButtonUpdatePWMPins_Click(object sender, RoutedEventArgs e)
        {
            TextBoxLastResponse.Text = netduino.UpdatePWMPins();
        }
    }
}
